/*
 *  Vec.cpp
 *  
 *
 *  Created by Daniel Condon on 23/08/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include <math.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "Vec.h"

using namespace std;

Vec::Vec(){
	Vec(0.0, 0.0);
}

Vec::Vec(double nx, double ny){
	x = nx;
	y = ny;
}

void Vec::setX(double nx){
	x = nx;
}

void Vec::setY(double ny){
	y = ny;
}

double Vec::getX(){
	return x;
}

double Vec::getY(){
	return y;
}

double Vec::getLen(){
	return sqrt(x*x+y*y);
}

Vec Vec::getUnit(){
	double l = getLen();
	Vec ret;
	if (l == 0.0) {
		ret.setX(0.0);
		ret.setY(0.0);
		return ret;
	}
	ret.setX(x/l);
	ret.setY(y/l);
	return ret;
}

Vec Vec::To(Vec other){
	Vec ret;
	ret.setX(x - other.getX());
	ret.setY(y - other.getY());
	return ret;
}

Vec Vec::From(Vec other){
	Vec ret(x, y);
	ret =  other - ret;
	return ret;
}

double Vec::Dist(Vec other){
	double a,b;
	a = x-other.getX();
	b = y-other.getY();
	return sqrt(a*a+b*b);
}

void Vec::Rotate(double theta){
	double nx,ny;
	nx = x*cos(theta)-y*sin(theta);
	ny = x*sin(theta)+y*cos(theta);
	x = nx;
	y = ny;
}

Vec Vec::operator+(Vec param){
	Vec tmp;
	tmp.setX(x+param.getX());
	tmp.setY(y+param.getY());
	return tmp;
}

Vec Vec::operator+=(Vec param){
	x += param.getX();
	y += param.getY();
}

Vec Vec::operator-(Vec param){
	Vec tmp;
	tmp.setX(x-param.getX());
	tmp.setY(y-param.getY());
	return tmp;
}

Vec Vec::operator-=(Vec param){
	x -= param.getX();
	y -= param.getY();
}

Vec Vec::operator*(double scalar){
	Vec ret;
	ret.setX(x*scalar);
	ret.setY(y*scalar);
	return ret;
}

Vec Vec::operator/(double scalar){
	Vec ret;
	ret.setX(x/scalar);
	ret.setY(y/scalar);
	return ret;
}

void Vec::clear(){
	x = 0.0;
	y = 0.0;
}